// 封装Promis
import { API_BASE_URL } from '../config/index.js'

export const Http = (url,data={},method='get') =>{
	return new Promise((reslove,reject)=>{
		uni.showLoading({
			title:'加载中....'
		})
		uni.request({
		    url : API_BASE_URL+url, //仅为示例，并非真实接口地址。
		    data,
			method,
		    header: {
		        'Authorization': uni.getStorageSync('token') //自定义请求头信息
		    },
		    success: (res) => {
				if(res.data.errCode==10000){
					reslove(res.data)
				}else if(res.data.errCode == 10200){
					 uni.navigateTo({
					 	url:'/pages/login/login',
					 })
				}
				// reject(res.data.errMsg)
				else{
					uni.showToast({
						icon:'error',
						title:res.data.errMsg
					});
					reject(res.data.errMsg)
				 }
				 uni.hideLoading()
		        console.log(res.data);
		    },
			fail:(error)=>{
				if(error.errMsg === 'request:fail'){
					uni.showToast({
						icon:'error',
						title:'网络错误',
						duration:'2000'
					})
				}
				uni.hideLoading()
				reject(error)
			},
			complete:() =>{
				uni.hideLoading()
				console.log('complele');
			}
		});
	})
}

export const get =(url,data={})=>{
	return Http(url,data,'get')
}
export const post =(url,data={})=>{
	return Http(url,data,'post')
}